﻿ko.Application.prototype.todos = ko.observableArray();
ko.Application.prototype.todoForEdit = ko.observable();

ko.Application.prototype.addTodo = function () {
    var self = ko.controller;

    self.todoForEdit( new ko.todoModel( { state: 1 } ) );
    $( '#modelModal' )
    .on( 'shown.bs.modal', function () {
        $( "#btnOK", this ).on( "click", function ( e ) {
            var model = self.todoForEdit();
            self.save( model, function () {
                $( '#modelModal' ).modal( 'hide' );
                ko.controller.todos.push( model );
            } );
        } );
    } )
    .on( 'hide.bs.modal', function () {
        $( "#btnOK", this ).off( "click" );
    } )
    .modal( 'show' );
}

ko.Application.prototype.TodoItemDragInit = function ( elem ) {
    // create an Event Object (http://arshaw.com/fullcalendar/docs/event_data/Event_Object/)
    var dom = elem.nextSibling;
    if ( dom && dom.nodeType == 1 ) {
        var eventObject = {
            title: $.trim( $( dom ).text() ),
            start: $( dom ).data( 'start' ),
            end: $( dom ).data( 'end' ),
            description: $( dom ).data( 'description' )
        };

        // store the Event Object in the DOM element so we can get to it later
        $( dom ).data( 'eventObject', eventObject );

        // make the event draggable using jQuery UI
        $( dom ).draggable( {
            zIndex: 999,
            revert: true,      // will cause the event to go back to its
            revertDuration: 0  //  original position after the drag
        } );
    }
};

ko.Application.prototype.init = function () {
    var self = this;
    self.load( 'todo', function () {
        //render To Calender

    } );
};

ko.todoModel = function ( todo ) {
    var self = this;
    self.__typeof = 'todo';

    self.id = todo.id;
    self.title = ko.observable( todo.title || '' );
    self.description = ko.observable( todo.description || '' );
    self.start = ko.observable( todo.start || '' );
    self.end = ko.observable( todo.end || '' );
    self.state = ko.observable( todo.state || 0 );

    self.event = {};
    self.event.TodoItemDragInit = function ( element ) {
        alert(1)
        $$.TodoItemDragInit( element );
    };
};

